package com.kehao.leetcode.algorithm;

import org.junit.Test;

public class Solution309 {
    public int maxProfit(int[] prices) {
        if(prices.length<2) return 0;
        int[] hasStock = new int[prices.length];
        int[] noStock = new int[prices.length];
        hasStock[0] = -prices[0];
        noStock[0] = 0;
        hasStock[1] = Math.max(hasStock[0],-prices[1]);
        noStock[1] = Math.max(0,hasStock[0]+prices[1]);
        for (int i = 2; i < prices.length; i++) {
            hasStock[i] = Math.max(hasStock[i-1],-prices[i]+noStock[i-2]);
            noStock[i] = Math.max(noStock[i-1],+prices[i]+hasStock[i-1]);
        }
        return Math.max(hasStock[prices.length-1],noStock[prices.length-1]);
    }

    @Test
    public void myTest(){
        System.out.println(maxProfit(new int[]{1, 2, 3, 0, 2}));
    }

}
